package com.example.weather.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.example.weather.pojo.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WeatherResponseUtil {
    Map<String, Object> mappedData = new HashMap<>();

    // 映射英文字段到中文意思的映射表
    private static final Map<String, String> fieldNamesMap = new HashMap<>();

    static {
//        fieldNamesMap.put("status", "返回状态");
//        fieldNamesMap.put("count", "返回结果总数目");
//        fieldNamesMap.put("info", "返回的状态信息");
//        fieldNamesMap.put("infocode", "返回状态说明");
        fieldNamesMap.put("lives", "实况天气数据信息列表");
        fieldNamesMap.put("forecast", "预报天气信息数据列表");
        fieldNamesMap.put("province", "省份名");
        fieldNamesMap.put("city", "城市名");
        fieldNamesMap.put("adcode", "区域编码");
        fieldNamesMap.put("weather", "天气现象");
        fieldNamesMap.put("temperature", "实时气温");
        fieldNamesMap.put("winddirection", "风向描述");
        fieldNamesMap.put("windpower", "风力级别");
        fieldNamesMap.put("humidity", "空气湿度");
        fieldNamesMap.put("reporttime", "数据发布时间");
        fieldNamesMap.put("date", "日期");
        fieldNamesMap.put("week", "星期");
        fieldNamesMap.put("dayweather", "白天天气现象");
        fieldNamesMap.put("nightweather", "晚上天气现象");
        fieldNamesMap.put("daytemp", "白天温度");
        fieldNamesMap.put("nighttemp", "晚上温度");
        fieldNamesMap.put("daywind", "白天风向");
        fieldNamesMap.put("nightwind", "晚上风向");
        fieldNamesMap.put("daypower", "白天风力");
        fieldNamesMap.put("nightpower", "晚上风力");
    }

    // 将 WeatherResponse 对象转换为带有中文字段名的 JSON 字符串
    public  String toJson(WeatherVo vo) {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        if (CollectionUtil.isNotEmpty(vo.getForecasts())){
            Map<String, Object> mappedData = mapToChineseFieldNamesWithForecasts(vo);
            return gson.toJson(mappedData);
        }
        if (CollectionUtil.isNotEmpty(vo.getLives())){
            Map<String, Object> mappedData = mapToChineseFieldNamesWithLives(vo);
            return gson.toJson(mappedData);
        }
        return gson.toJson(vo);
    }

    // 将字段名映射为中文意思，并生成 Map 对象
    private  Map<String, Object> mapToChineseFieldNamesWithLives(WeatherVo response) {

//        mappedData.put(fieldNamesMap.get("status"), response.getStatus());
//        mappedData.put(fieldNamesMap.get("count"), response.getCount());
//        mappedData.put(fieldNamesMap.get("info"), response.getInfo());
//        mappedData.put(fieldNamesMap.get("infocode"), response.getInfocode());

        // 处理实况天气信息
        List<Map<String, Object>> liveWeathers = new ArrayList<>();
        for (LiveWeather liveWeather : response.getLives()) {
            Map<String, Object> mappedWeather = new HashMap<>();
            mappedWeather.put(fieldNamesMap.get("province"), liveWeather.getProvince());
            mappedWeather.put(fieldNamesMap.get("city"), liveWeather.getCity());
            mappedWeather.put(fieldNamesMap.get("adcode"), liveWeather.getAdcode());
            mappedWeather.put(fieldNamesMap.get("weather"), liveWeather.getWeather());
            mappedWeather.put(fieldNamesMap.get("temperature"), liveWeather.getTemperature());
            mappedWeather.put(fieldNamesMap.get("winddirection"), liveWeather.getWinddirection());
            mappedWeather.put(fieldNamesMap.get("windpower"), liveWeather.getWindpower());
            mappedWeather.put(fieldNamesMap.get("humidity"), liveWeather.getHumidity());
            mappedWeather.put(fieldNamesMap.get("reporttime"), liveWeather.getReporttime());
            liveWeathers.add(mappedWeather);
        }
        mappedData.put(fieldNamesMap.get("lives"), liveWeathers);
        return mappedData;
}
    private  Map<String, Object> mapToChineseFieldNamesWithForecasts(WeatherVo response) {

        // 处理预报天气信息
        List<Map<String, Object>> forecasts = new ArrayList<>();
        for (Forecast forecast : response.getForecasts()) {
            Map<String, Object> mappedForecast = new HashMap<>();
            mappedForecast.put(fieldNamesMap.get("city"), forecast.getCity());
            mappedForecast.put(fieldNamesMap.get("adcode"), forecast.getAdcode());
            mappedForecast.put(fieldNamesMap.get("province"), forecast.getProvince());
            mappedForecast.put(fieldNamesMap.get("reporttime"), forecast.getReporttime());
            // 处理预报数据列表
            List<Map<String, Object>> casts = new ArrayList<>();
            for (Cast cast : forecast.getCasts()) {
                Map<String, Object> mappedCast = new HashMap<>();
                mappedCast.put(fieldNamesMap.get("date"), cast.getDate());
                mappedCast.put(fieldNamesMap.get("week"), cast.getWeek());
                mappedCast.put(fieldNamesMap.get("dayweather"), cast.getDayweather());
                mappedCast.put(fieldNamesMap.get("nightweather"), cast.getNightweather());
                mappedCast.put(fieldNamesMap.get("daytemp"), cast.getDaytemp());
                mappedCast.put(fieldNamesMap.get("nighttemp"), cast.getNighttemp());
                mappedCast.put(fieldNamesMap.get("daywind"), cast.getDaywind());
                mappedCast.put(fieldNamesMap.get("nightwind"), cast.getNightwind());
                mappedCast.put(fieldNamesMap.get("daypower"), cast.getDaypower());
                mappedCast.put(fieldNamesMap.get("nightpower"), cast.getNightpower());
                casts.add(mappedCast);
            }
            mappedForecast.put(fieldNamesMap.get("casts"), casts);
            forecasts.add(mappedForecast);
        }
        mappedData.put(fieldNamesMap.get("forecast"), forecasts);

        return mappedData;
    }
}